package com.jboltai.util.text;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

import java.nio.file.Path;
import java.nio.file.Paths;


public class PathUtil {
    private static final Path TMP_DIR = Paths.get(System.getProperty("java.io.tmpdir"));

    public static Path createTempFilePath() {
        Path filePath = Paths.get(TMP_DIR.toString(), "tmp_" + RandomUtil.randomNumbers(19) + ".tmp");
        return filePath;
    }

    public static boolean isImage(String suffix) {
        if (StrUtil.isBlank(suffix)) return false;
        switch (suffix.toLowerCase()) {
            case "jpg":
            case "jpeg":
            case "png":
            case "gif":
            case "bmp":
            case "tif":
            case "tiff":
            case "webp":
            case "ico":
                return true;
        }
        return false;
    }

    public static boolean isVideo(String suffix) {
        if (StrUtil.isBlank(suffix)) return false;
        switch (suffix.toLowerCase()) {
            case "mp4":
            case "avi":
            case "mov":
            case "flv":
            case "wmv":
            case "rm":
            case "rmvb":
            case "mkv":
                return true;
        }
        return false;
    }

    public static boolean isAudio(String suffix) {
        if (StrUtil.isBlank(suffix)) return false;
        switch (suffix.toLowerCase()) {
            case "mp3":
            case "wav":
            case "aac":
            case "flac":
            case "ogg":
            case "wma":
                return true;
        }
        return false;
    }

    public static String getSuffix(String contentType, String resourceName) {
        if (contentType == null) {
            // 不是图片
            if (StrUtil.isBlank(resourceName)) {
                return "";
            }
            return resourceName.substring(resourceName.lastIndexOf(".") + 1, resourceName.length());
        } else {
            // 未知类型, 暂不处理
            if (!contentType.startsWith("image/")) {
                return "";
            }
            // 这种是媒体文件的图标
            if ("image/x-emf".equals(contentType)) {
                return "";
            }
            // 这种是图片
            return contentType.substring(contentType.indexOf("/") + 1);
        }
    }

    public static String getSuffixByResourceName(String resourceName) {
        if (StrUtil.isBlank(resourceName)) {
            return "";
        }
        return resourceName.substring(resourceName.lastIndexOf(".") + 1, resourceName.length());
    }
}
